import 'package:flutter/material.dart';
import 'package:native_context_menu/native_context_menu.dart';

class MouseMenu extends StatefulWidget {
  const MouseMenu({
    required this.items,
    required this.child,
    Key? key,
  }) : super(key: key);

  final List<PopupMenuItem> items;
  final Widget child;

  @override
  _MouseMenuState createState() => _MouseMenuState();
}

class _MouseMenuState extends State<MouseMenu> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: widget.child,
      onSecondaryTapDown: (details) {
        showMenu(
          context: context,
          position: RelativeRect.fromRect(Rect.fromLTRB(details.globalPosition.dx, details.globalPosition.dy, details.globalPosition.dx + 110, details.globalPosition.dy - 40), Offset.zero & (Overlay.of(context).context.findRenderObject() as RenderBox).size),
          items: widget.items,
        );
      },
    );
  }
}

class MouseNativeMenu extends StatefulWidget {
  const MouseNativeMenu({
    required this.items,
    required this.onItemSelected,
    required this.child,
    Key? key,
  }) : super(key: key);

  final List<MenuItem> items;
  final void Function(MenuItem item)? onItemSelected;
  final Widget child;

  @override
  _MouseNativeMenuState createState() => _MouseNativeMenuState();
}

class _MouseNativeMenuState extends State<MouseNativeMenu> {
  @override
  Widget build(BuildContext context) {
    return ContextMenuRegion(
      child: widget.child,
      menuItems: widget.items,
      onItemSelected: widget.onItemSelected,
    );
  }
}
